package com.gmako.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import com.gmako.entity.Activityinfo;
import com.gmako.entity.Packageinfo;
import com.gmako.service.ActivityinfoService;
import com.gmako.service.PackageinfoService;
import com.gmako.utils.Constants;
import com.gmako.utils.Pager;
@Controller
@RequestMapping("/index.do")
public class IndexController {
	@Autowired(required=false) 
	private ActivityinfoService activityinfoService;
	@Autowired(required=false) 
	private PackageinfoService packageinfoService;
	
	/**
	 * 跳转首页
	 * @param request
	 * @param type
	 * @return
	 */
	@RequestMapping
	public String jumpForIndex(HttpServletRequest request,@RequestParam("type") String type,@RequestParam("pageNo") String pageNos){
		int pageNo = Integer.parseInt(pageNos);
		if (request.getParameter("pageNo") != null
				&& !"".equals(request.getParameter("pageNo"))) {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));

		} else {
			pageNo = Constants.pageNo;
		}
		Pager p=activityinfoService.findPagerHql(Constants.INDEX, pageNo,Integer.parseInt(type));
		List<Activityinfo> li=p.getResultList();
		for (Activityinfo activityinfo : li) {
			Packageinfo pa= packageinfoService.getPackageinfo(activityinfo.getPackageInfoId());
			activityinfo.setPackageName(pa.getPackageName());
			activityinfo.setPrice(pa.getPrice().toString());
		}
		request.setAttribute("pager", p);
		request.setAttribute("type", type);
		return "index";
	}
	
	
	/**
	 * 用户登录主页
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "method=loginMain")
	public String loginMainPage(@RequestParam("name") String name , HttpServletRequest request){
		int pageNo = 0;
		if (request.getParameter("pageNo") != null
				&& !"".equals(request.getParameter("pageNo"))) {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));

		} else {
			pageNo = Constants.pageNo;
		}
		Pager p=activityinfoService.findPagerHql(Constants.INDEX, pageNo, 1);
		List<Activityinfo> li=p.getResultList();
		for (Activityinfo activityinfo : li) {
			System.out.println(activityinfo.getActivityinfoName());
		}
		System.out.println(p.getResultList().size());
		System.out.println(pageNo+"pageNo");
		request.setAttribute("pager", p);
		request.setAttribute("name", name);
		request.setAttribute("needLogin", 1);
		System.out.println("登录 主页啦########"+name);
		return "index";
	}	

}
